/* 
 * JUGJobs -- A Jobs Posting Application for Java Users Groups and Other Groups
 * $Id: CheckAdminLoginAction.java,v 1.3 2006/06/26 04:57:04 biglee Exp $
 * 
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the JUGJobs project.
 *
 * The Initial Developers of the Original Code are the members of the Triangle
 * Java User's Group in the RTP area of North Carolina.
 * Portions created by the Initial Developer are Copyright (C) 2005
 * the Initial Developers. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** 
 */
package org.jugjobs.action;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * @author BigLee
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class CheckAdminLoginAction extends CommonAction {
	  
	  public ActionForward execute(ActionMapping mapping, ActionForm form,
	      HttpServletRequest request, HttpServletResponse response)
	  { 
	    ActionErrors errors = new ActionErrors();
	  	String adminUserName = servlet.getInitParameter("adminUserName");
	    String adminPassword = servlet.getInitParameter("adminPassword");
	    if (adminUserName == null || adminPassword == null)
	    {
	     logger.log("CheckAdminLoginAction, " +
	          "either adminUserName or adminPassword not found in web.xml");
	      errors.add("configuration", new ActionError("jugjobs.admin.text.configuration"));
	    }
	    String submittedUserName = request.getParameter("j_username");
	    String submittedPassword = request.getParameter("j_password");
	    String logString = "CheckAdminLoginAction, " +
	      "administrator log in: username \""+submittedUserName
	      +"\" adminPassword "+submittedPassword.length()+" chars ";
	    if(adminUserName.equals(submittedUserName) &&
	       adminPassword.equals(submittedPassword))
	    {
	      request.getSession().setAttribute(ADMIN_LOGGED_IN, ADMIN_LOGGED_IN);
	      logger.log(logString + " SUCCEEDED");
	    }
	    else
	    {
	    	logger.log(logString + " FAILED");
	      errors.add("badPassword", new ActionError("jugjobs.admin.text.badPassword"));
	    }
      
	    if (errors.isEmpty()) { return mapping.findForward("jobList"); }
	    else { saveErrors(request, errors);
	    	   return mapping.getInputForward();
	    	 }
	    
	  }
	}
